<template>
  <div class="pieChart">
    <div :id="pieData.pieId" :style="{ height: height, width: width }"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
import colorConst from "@/utils/custom-color.js";
echarts.use([PieChart]);
export default {
  name: "pieChart",
  props: {
    // 饼图数据
    pieData: {
      type: Object,
      default: {},
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "150px",
    },
  },
  data() {
    return {
      chart: null,
      pieValue: null,
    };
  },
  mounted() {
    // setTimeout(() => {
    this.pieCharts(this.pieData);
    // }, 600);
  },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    pieCharts(pieData) {
      let unit = pieData.unit ? pieData.unit : "";
      let option = {
        color: colorConst.overviewPublicInstitutions,
        title: {
          text: pieData.title,
          textStyle: {
            fontSize: 20,
            fontWeight: 400,
          },
          // subtext: '副标题',
          left: "center",
          // top: "5%",
        },
        tooltip: {
          show: true,
          trigger: "item",
          formatter: "{b}:{c}" + unit,
        },
        legend: {
          type: "scroll",
          orient: "vertical",
          top: "23%",
          padding: 0,
          right: "1%",
          icon: "circle",
          itemGap: 3,
          textStyle: {
            color: "#8C8C8C",
            fontFamily: "PingFang-SC-Regular, PingFang-SC",
            fontWeight: 400,
            lineHeight: 17,
          },
        },
        series: [
          {
            name: pieData.title,
            type: "pie",
            top: "10",
            radius: pieData.radius,
            roseType: pieData.roseType ? pieData.roseType : "",
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: pieData.borderRadius ? pieData.borderRadius : "", // 设置圆角
              borderColor: "#fff",
              borderWidth: 1,
            },
            label: {
              show: false,
              position: pieData.position,
              color: "#1890FF",
              formatter: function (params) {
                return params.value; // 中间显示数据
              },
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "20",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: pieData.dataArr,
          },
        ],
      };
      this.initChart(pieData.pieId, option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
      // this.chart.on("mouseover", (params) => {
      //   this.pieValue = params.value;
      //   console.log(this.pieValue, 123);
      // });
    },
  },
};
</script>
